<%@ page pageEncoding="UTF-8"%>
<%@ include file="baduserateDept.jsp"%>
<%!
// 将map按照时间字段值降序
	class MapComparator implements Comparator<Map> {
		@Override
		public int compare(Map map1, Map map2) {
			double time1 = 0;
			double time2 = 0;
			try {

				time1 = Double.parseDouble(Tools.getDesc(map1.get("cont").toString()));
				time2 = Double.parseDouble(Tools.getDesc(map2.get("cont").toString()));
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return (time1 > time2) ? -1 : (time1 == time2) ? 0 : 1;
		}
	}
public Map getMap(List<Map> result , Map<String,String> deptMap ){
		
		int sumCount = 0 ;
		//处理结合数据
		for(Map map : result){
			String ksdm = map.get("ksdm") == null ? "0" : map.get("ksdm").toString();
			map.put("deptName",deptMap.get(ksdm));
			map.put("cont",map.get("cont").toString()+"%");
			
			sumCount++;
		}
		
		Map<String,Object> resMap = new HashMap<String,Object>();
		resMap.put("rank",result);
		resMap.put("sumCount",sumCount);
		return resMap;
	}
%>

<%
	//查询床位占用率
	String yydm =  input.getDesc("yydm","01");//医院代码	
	String date = input.getDesc("start_date","2018-01-11");//开始时间
	String endDa = input.getDesc("end_date","2018-01-11");//结束时间

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	String nowDate = sdf2.format(sdf.parse(date));
	String nowEndDa = sdf2.format(sdf.parse(endDa));
	
	String startDate = nowDate+"00:00:00";
	String endDate = nowEndDa+"23:59:59";
	//返回参数
	DecimalFormat df = new DecimalFormat("######0.00"); 
	Message msg = new Message();
	//当前使用数量
	double currentCount = 0;
	double sumCount = 1;
	String sqlWhere = "";
	String hql4Where = " ";
	if(!yydm.equals("-1"))
	sqlWhere += " and ksdm in (select id  from his_dept where yydm ='"+yydm+"') " ;
	
	//获取所有科室
	List<Map> deptList = QBuilder.db("basedb").queryHql("select new map(id as id , name as name , yydm as yydm ) from his_dept ");
	Map<String,String> deptMap1 = new HashMap<String,String>();
	//科室放到map中 key-value(id-name)
	for(Map map : deptList){
		deptMap1.put(map.get("id").toString(),map.get("name").toString());
	}
	
	

	hql4Where += " group by ksdm , ksmc";
	String hql3 = "select new map(count(a) as count ,a.ksdm as ksdm) from ZY_BRSYK a where  cwdm !='' and cwdm is not null and (( cyrq >= '" + endDate + "' and ryrq <= '" + endDate + "' ) or (cyrq is null and ryrq <= '" + startDate + "')) "+sqlWhere+"  group by ksdm having count(a) >0  order by count(a) desc";
	List<Map> result3 = QBuilder.db("basedb").queryHql(hql3);
	
	//查询床位总数

	Map<String ,Object> cwMap = new HashMap<String ,Object>();
	Map<String ,Object> deptMap = new HashMap<String ,Object>();
	for(Map map : deptList){
		String id = map.get("id").toString();
		String name = map.get("name").toString();
		if(amap.get(id) == null ){
			continue;
		}
		cwMap.put(id,amap.get(id));
		deptMap.put(id, name);
	}
	
	List<Map> resultMap = new ArrayList<Map>(); 
	for(Map map : result3){
		double count = Double.parseDouble(map.get("count").toString());
		if(cwMap.get(map.get("ksdm").toString()) == null){
			//如果上面没有找到此科室 则过滤掉
			continue;
		}
		if(map.get("ksdm") == null || cwMap.get(map.get("ksdm")) == null || "null".equals(cwMap.get(map.get("ksdm")).toString()) ||cwMap.get(map.get("ksdm")).toString() == null ){
			map.put("cont","0");
			continue;
		}
		double sum = Double.parseDouble(cwMap.get(map.get("ksdm").toString()).toString());
		
		map.put("cont",df.format(count/sum*100));
		map.put("ksmc",deptMap.get(map.get("ksdm").toString()));
		resultMap.add(map);
	}
	Collections.sort(resultMap, new MapComparator());
	Map<String,Object> m1 = new HashMap<String,Object>();
	
	m1.put("ksMap",getMap(resultMap , deptMap1 ));
	msg.put("errcode",0);
	msg.put("zy_baduserate",m1);
	
	
	
	
	
	
	out.println(msg);
%>